package lockutil

import (
	"github.com/cihub/seelog"
	"shop/dao"
	"time"
)

// TryLock 分布式锁
func TryLock(key, v string, ttlSeconds int) bool {
	seelog.Tracef("TryLock, key=%s, v=%s", key, v)
	return dao.GetRedisCli().SetNX(key, v, time.Duration(ttlSeconds)*time.Second).Val()
}

func ReleaseLock(key, v string) bool {
	seelog.Tracef("ReleaseLock, key=%s, v=%s", key, v)
	if dao.GetRedisCli().Get(key).Val() == v { // 非原子操作
		return dao.GetRedisCli().Del(key).Val() == 1
	}
	return false
}
